package com.bookseek.xreader.dbhelper;

import android.content.Context;
import android.util.Log;

import com.bookseek.xreader.consts.XreaderConts;
import com.bookseek.xreader.dao.DaoMaster;
import com.bookseek.xreader.dao.DaoSession;

import org.greenrobot.greendao.database.Database;

public class DbSessionUtils {
    private static DaoSession DAO_SESSION;
    private static Database DB;

    public synchronized static void init(Context context, String dbname){
        if(DAO_SESSION == null){
            Log.i(XreaderConts.TAG,"初始化===>DB,DB_SESSION");
            DxmOpenHelper dxmOpenHelper = new DxmOpenHelper(context,dbname);
            DB = dxmOpenHelper.getWritableDb();
            //DB = dxmOpenHelper.getEncryptedWritableDb("ll11llo");
            DAO_SESSION = new DaoMaster(DB).newSession();
        }
    }

    public synchronized static void initV1(Context context, String dbname){
        if(DAO_SESSION == null){
            Log.i(XreaderConts.TAG,"初始化===>DB,DB_SESSION");
            DxmOpenHelper dxmOpenHelper = new DxmOpenHelper(context,dbname);
            //DB = dxmOpenHelper.getWritableDb();
            DB = dxmOpenHelper.getEncryptedWritableDb("ll11llo");
            DAO_SESSION = new DaoMaster(DB).newSession();
        }
    }

    public static DaoSession genDaoSession(){
        return DAO_SESSION;
    }

    //shici+dianji cid等于rowid
    public static void initBdocTable(Database db){
        String createFtsTableSql = "CREATE VIRTUAL TABLE IF NOT EXISTS bdoc " +
                "USING fts5(" +
                "bname , " +
                "bauthor, " +
                "cname, " +
                "cont, " +
                "bchao, " +
                "btype, " +
                "cimg UNINDEXED, " +
                "nword, " +
                "bid , " +
                "cid UNINDEXED, " +
                "bphk UNINDEXED, " +
                "cphk UNINDEXED, " +
                "tokenize = 'unicode61'" +  // 默认分词器（支持多语言，含中文）
                ")";
        db.execSQL(createFtsTableSql);
        Log.i(XreaderConts.TAG,"初始化fts5-bdoc 表");
    }
    public static void initBookTable(Database db){
        String createFtsTableSql = "CREATE VIRTUAL TABLE IF NOT EXISTS book " +
                "USING fts5(" +
                "cname , " +
                "cont, " +
                "bid , " +
                "cid UNINDEXED, " +
                "tokenize = 'unicode61'" +  // 默认分词器（支持多语言，含中文）
                ")";
        db.execSQL(createFtsTableSql);
        Log.i(XreaderConts.TAG,"初始化fts5-book 表");
    }

//    CREATE VIRTUAL TABLE bdoc USING fts5(
//            cname,
//            cont,
//            bid ,
//            cid UNINDEXED
//    );


    //古籍fts5表
    public static void initBdocgjTable(Database db){

    }



}
